{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import train_test_split\n",
    "from time import time"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_path = \"D:\\\\大三下\\\\NLP_Foundation\\\\datasets\\\\IMDB Dataset.csv\"\n",
    "imdb_data = pd.read_csv(data_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "                                                   review sentiment\ncount                                               50000     50000\nunique                                              49582         2\ntop     Loved today's show!!! It was a variety and not...  positive\nfreq                                                    5     25000",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>review</th>\n      <th>sentiment</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>count</th>\n      <td>50000</td>\n      <td>50000</td>\n    </tr>\n    <tr>\n      <th>unique</th>\n      <td>49582</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>top</th>\n      <td>Loved today's show!!! It was a variety and not...</td>\n      <td>positive</td>\n    </tr>\n    <tr>\n      <th>freq</th>\n      <td>5</td>\n      <td>25000</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_data.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "imdb_data['num_sentiment'] = imdb_data['sentiment'].map({'positive': 1, 'negative': 0})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "                                              review sentiment  num_sentiment\n0  One of the other reviewers has mentioned that ...  positive              1\n1  A wonderful little production. <br /><br />The...  positive              1\n2  I thought this was a wonderful way to spend ti...  positive              1\n3  Basically there's a family where a little boy ...  negative              0\n4  Petter Mattei's \"Love in the Time of Money\" is...  positive              1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>review</th>\n      <th>sentiment</th>\n      <th>num_sentiment</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>One of the other reviewers has mentioned that ...</td>\n      <td>positive</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>A wonderful little production. &lt;br /&gt;&lt;br /&gt;The...</td>\n      <td>positive</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>I thought this was a wonderful way to spend ti...</td>\n      <td>positive</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Basically there's a family where a little boy ...</td>\n      <td>negative</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Petter Mattei's \"Love in the Time of Money\" is...</td>\n      <td>positive</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imdb_data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../../docs/thucnews/stopwords/baidu_stopwords.txt'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mFileNotFoundError\u001B[0m                         Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-6-14fbc4506d00>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[1;31m# 加载停用词\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[0mstopwords_path\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;34m'../../docs/thucnews/stopwords/baidu_stopwords.txt'\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 3\u001B[1;33m \u001B[1;32mwith\u001B[0m \u001B[0mopen\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mstopwords_path\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mencoding\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;34m'utf-8'\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;32mas\u001B[0m \u001B[0mfile\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      4\u001B[0m     \u001B[0mstopwords\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mfile\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0msplit\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m\"\\n\"\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: '../../docs/thucnews/stopwords/baidu_stopwords.txt'"
     ]
    }
   ],
   "source": [
    "# 加载停用词\n",
    "stopwords_path = '../../docs/thucnews/stopwords/baidu_stopwords.txt'\n",
    "with open(stopwords_path, encoding='utf-8') as file:\n",
    "    stopwords = file.read().split(\"\\n\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "\n",
    "# TfidfVectorizer可以把原始文本转化为tf-idf的特征矩阵，从而为后续的文本相似度计算，主题模型(如LSI)，文本搜索排序等一系列应用奠定基础\n",
    "tfidf = TfidfVectorizer(stop_words=stopwords)\n",
    "# tfidf.fit_transform 参数为list ,返回结果类型 scipy.sparse.csr.csr_matrix\n",
    "X = tfidf.fit_transform(imdb_data['review'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(len(imdb_data))\n",
    "X.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, imdb_data['sentiment'], random_state=41)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "from sklearn import metrics\n",
    "\n",
    "\n",
    "def run_assess(model_name, predict_test, y_test):\n",
    "    print(model_name + \"文本分类的准确率为：\", metrics.accuracy_score(predict_test, y_test))\n",
    "    print(model_name + \"文本分类的精度率为：\", metrics.precision_score(predict_test, y_test, average='weighted'))\n",
    "    print(model_name + \"文本分类的召回率为：\", metrics.recall_score(predict_test, y_test, average='weighted'))\n",
    "    print(model_name + \"文本分类的F1值为：\", metrics.f1_score(predict_test, y_test, average='weighted'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多项式朴素贝叶斯文本分类的准确率为： 0.83264\n",
      "多项式朴素贝叶斯文本分类的精度率为： 0.8326406607101317\n",
      "多项式朴素贝叶斯文本分类的召回率为： 0.83264\n",
      "多项式朴素贝叶斯文本分类的F1值为： 0.8326395629863348\n"
     ]
    }
   ],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB\n",
    "\n",
    "#多项式朴素贝叶斯\n",
    "nb_model = MultinomialNB(alpha=0.001)\n",
    "nb_model.fit(X_train, y_train)\n",
    "nb_predict = nb_model.predict(X_test)\n",
    "\n",
    "model_name = \"多项式朴素贝叶斯\"\n",
    "run_assess(model_name, nb_predict, y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bernoulli贝叶斯文本分类的准确率为： 0.82432\n",
      "bernoulli贝叶斯文本分类的精度率为： 0.8260528588442196\n",
      "bernoulli贝叶斯文本分类的召回率为： 0.82432\n",
      "bernoulli贝叶斯文本分类的F1值为： 0.824456445221199\n"
     ]
    }
   ],
   "source": [
    "#bernoulli朴素贝叶斯\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "\n",
    "ber_model = BernoulliNB(alpha=0.001)\n",
    "ber_model.fit(X_train, y_train)\n",
    "ber_predict = ber_model.predict(X_test)\n",
    "\n",
    "model_name = \"bernoulli贝叶斯\"\n",
    "run_assess(model_name, ber_predict, y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "complement贝叶斯文本分类的准确率为： 0.83264\n",
      "complement贝叶斯文本分类的精度率为： 0.832640030867874\n",
      "complement贝叶斯文本分类的召回率为： 0.83264\n",
      "complement贝叶斯文本分类的F1值为： 0.8326396743813748\n"
     ]
    }
   ],
   "source": [
    "# Complement朴素贝叶斯\n",
    "from sklearn.naive_bayes import ComplementNB\n",
    "\n",
    "com_model = ComplementNB(alpha=0.001)\n",
    "com_model.fit(X_train, y_train)\n",
    "com_predict = com_model.predict(X_test)\n",
    "\n",
    "model_name = \"complement贝叶斯\"\n",
    "run_assess(model_name, com_predict, y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "决策树文本分类的准确率为： 0.7272\n",
      "决策树文本分类的精度率为： 0.7272582073649074\n",
      "决策树文本分类的召回率为： 0.7272\n",
      "决策树文本分类的F1值为： 0.7272036175643914\n",
      "execute time: 184.76862716674805 s\n"
     ]
    }
   ],
   "source": [
    "# 决策树\n",
    "start = time()\n",
    "\n",
    "from sklearn import tree\n",
    "\n",
    "dtc = tree.DecisionTreeClassifier()\n",
    "dtc = dtc.fit(X_train, y_train)\n",
    "dtc_predict = dtc.predict(X_test)\n",
    "\n",
    "model_name = \"决策树\"\n",
    "run_assess(model_name, dtc_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最近邻文本分类的准确率为： 0.76264\n",
      "最近邻文本分类的精度率为： 0.7671934551873996\n",
      "最近邻文本分类的召回率为： 0.76264\n",
      "最近邻文本分类的F1值为： 0.7632133815525067\n",
      "execute time: 48.159186363220215 s\n"
     ]
    }
   ],
   "source": [
    "# KNN\n",
    "start = time()\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "knn = KNeighborsClassifier(n_neighbors=3)\n",
    "knn = knn.fit(X_train, y_train)\n",
    "knn_predict = knn.predict(X_test)\n",
    "\n",
    "model_name = \"最近邻\"\n",
    "run_assess(model_name, knn_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "多层感知机文本分类的准确率为： 0.87912\n",
      "多层感知机文本分类的精度率为： 0.8791211254342491\n",
      "多层感知机文本分类的召回率为： 0.87912\n",
      "多层感知机文本分类的F1值为： 0.879120287022632\n",
      "execute time: 161.67256426811218 s\n"
     ]
    }
   ],
   "source": [
    "# 神经网络多层感知机\n",
    "start = time()\n",
    "\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "\n",
    "mlpc = MLPClassifier(alpha=1e-05, hidden_layer_sizes=(15,), random_state=1,\n",
    "                     solver='lbfgs')\n",
    "mlpc.fit(X_train, y_train)\n",
    "mlpc_predict = mlpc.predict(X_test)\n",
    "\n",
    "model_name = \"多层感知机\"\n",
    "run_assess(model_name, mlpc_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林文本分类的准确率为： 0.77568\n",
      "随机森林文本分类的精度率为： 0.7818241028339302\n",
      "随机森林文本分类的召回率为： 0.77568\n",
      "随机森林文本分类的F1值为： 0.776335140079285\n",
      "execute time: 55.39717769622803 s\n"
     ]
    }
   ],
   "source": [
    "# 随机森林\n",
    "start = time()\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "rfc = RandomForestClassifier(n_estimators=10).fit(X_train, y_train)\n",
    "rfc_predict = rfc.predict(X_test)\n",
    "\n",
    "model_name = \"随机森林\"\n",
    "run_assess(model_name, rfc_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LogisticRegression文本分类的准确率为： 0.89344\n",
      "LogisticRegression文本分类的精度率为： 0.8940068373221617\n",
      "LogisticRegression文本分类的召回率为： 0.89344\n",
      "LogisticRegression文本分类的F1值为： 0.8934760454619458\n",
      "execute time: 5.52446985244751 s\n"
     ]
    }
   ],
   "source": [
    "# LogisticRegression\n",
    "start = time()\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "log_model = LogisticRegression().fit(X_train, y_train)\n",
    "log_predict = log_model.predict(X_test)\n",
    "\n",
    "model_name = \"LogisticRegression\"\n",
    "run_assess(model_name, log_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LightGBM文本分类的准确率为： 0.85704\n",
      "LightGBM文本分类的精度率为： 0.857553344596974\n",
      "LightGBM文本分类的召回率为： 0.85704\n",
      "LightGBM文本分类的F1值为： 0.8570838158428268\n",
      "execute time: 42.539347410202026 s\n"
     ]
    }
   ],
   "source": [
    "# LGBMClassifier\n",
    "start = time()\n",
    "\n",
    "from lightgbm import LGBMClassifier\n",
    "from sklearn.multiclass import OneVsRestClassifier\n",
    "\n",
    "clf_multilabel = OneVsRestClassifier(LGBMClassifier())\n",
    "\n",
    "clf_multilabel.fit(X_train, y_train)\n",
    "lgbm_predict = clf_multilabel.predict(X_test)\n",
    "\n",
    "model_name = \"LightGBM\"\n",
    "run_assess(model_name, lgbm_predict, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Documents\\anaconda3\\envs\\ml\\lib\\site-packages\\xgboost\\sklearn.py:1224: UserWarning: The use of label encoder in XGBClassifier is deprecated and will be removed in a future release. To remove this warning, do the following: 1) Pass option use_label_encoder=False when constructing XGBClassifier object; and 2) Encode your labels (y) as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1].\n",
      "  warnings.warn(label_encoder_deprecation_msg, UserWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[13:11:17] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.1/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "xgboost文本分类的准确率为： 0.85208\n",
      "xgboost文本分类的精度率为： 0.8537609667755884\n",
      "xgboost文本分类的召回率为： 0.85208\n",
      "xgboost文本分类的F1值为： 0.8522177883464545\n",
      "execute time: 37.77304172515869 s\n"
     ]
    }
   ],
   "source": [
    "# xgboost\n",
    "start = time()\n",
    "\n",
    "import xgboost as xgb\n",
    "\n",
    "xgb_clf = xgb.XGBClassifier().fit(X_train, y_train)\n",
    "\n",
    "xgb_clf_pred = xgb_clf.predict(X_test)\n",
    "\n",
    "model_name = \"xgboost\"\n",
    "run_assess(model_name, xgb_clf_pred, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SVM文本分类的准确率为： 0.90016\n",
      "SVM文本分类的精度率为： 0.9005946921009184\n",
      "SVM文本分类的召回率为： 0.90016\n",
      "SVM文本分类的F1值为： 0.9001865237724915\n",
      "execute time: 5688.025681018829 s\n"
     ]
    }
   ],
   "source": [
    "#  svm\n",
    "start = time()\n",
    "\n",
    "from sklearn import svm\n",
    "\n",
    "svm_clf = svm.SVC(decision_function_shape='ovo').fit(X_train, y_train)\n",
    "svm_pred = svm_clf.predict(X_test)\n",
    "\n",
    "model_name = \"SVM\"\n",
    "run_assess(model_name, svm_pred, y_test)\n",
    "\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "47/47 [==============================] - 209s 4s/step - loss: 0.6265 - accuracy: 0.6719\n",
      "Epoch 2/10\n",
      "47/47 [==============================] - 211s 4s/step - loss: 0.4824 - accuracy: 0.7769\n",
      "Epoch 3/10\n",
      "47/47 [==============================] - 225s 5s/step - loss: 0.3986 - accuracy: 0.8264\n",
      "Epoch 4/10\n",
      "47/47 [==============================] - 223s 5s/step - loss: 0.3439 - accuracy: 0.8583\n",
      "Epoch 5/10\n",
      "47/47 [==============================] - 218s 5s/step - loss: 0.3012 - accuracy: 0.8802\n",
      "Epoch 6/10\n",
      "47/47 [==============================] - 230s 5s/step - loss: 0.2663 - accuracy: 0.8995\n",
      "Epoch 7/10\n",
      "47/47 [==============================] - 222s 5s/step - loss: 0.2364 - accuracy: 0.9139\n",
      "Epoch 8/10\n",
      "47/47 [==============================] - 220s 5s/step - loss: 0.2081 - accuracy: 0.9291\n",
      "Epoch 9/10\n",
      "47/47 [==============================] - 215s 5s/step - loss: 0.1851 - accuracy: 0.9406\n",
      "Epoch 10/10\n",
      "47/47 [==============================] - 220s 5s/step - loss: 0.1630 - accuracy: 0.9518\n",
      "TextCNN模型文本分类的准确率为： 0.86224\n",
      "TextCNN模型文本分类的精度率为： 0.8622967869814512\n",
      "TextCNN模型文本分类的召回率为： 0.86224\n",
      "TextCNN模型文本分类的F1值为： 0.8622381731830258\n",
      "execute time: 2329.598360300064 s\n"
     ]
    }
   ],
   "source": [
    "from jiquanquan.tests.text_classification import text_cnn_model\n",
    "\n",
    "start = time()\n",
    "model_path = '../../docs/text_classification/imdb_result/imdb_cnn_model.h5'\n",
    "text_cnn_model.TextCNN_Model(imdb_data['review'], imdb_data['num_sentiment'],2, model_path)\n",
    "end = time()\n",
    "print(\"execute time: {} s\".format(end - start))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "conda-env-ml-py",
   "language": "python",
   "display_name": "Python [conda env:ml] *"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}